package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.dao.CommunMemberMapper;
import com.example.blog.dao.LiveCompanyMapper;
import com.example.blog.dto.AddMemberReqDto;
import com.example.blog.dto.QueryMemberResDto;
import com.example.blog.dto.UpdateMemberReqDto;
import com.example.blog.dto.base.IdRequestDto;
import com.example.blog.entity.CommunMember;
import com.example.blog.entity.LiveCompany;
import com.example.blog.service.CommunMemberService;
import com.example.blog.util.ParamUtil;
import com.example.blog.util.ToolsUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Admin
 */
@Service
public class CommunMemberServiceImpl extends ServiceImpl<CommunMemberMapper, CommunMember> implements CommunMemberService {

    @Override
    public Boolean addMember(AddMemberReqDto reqDto){
        if(checkMember(reqDto.getNumberId(),reqDto.getRoomId())){
            UpdateMemberReqDto updateMemberReqDto = ToolsUtil.convertType(reqDto,UpdateMemberReqDto.class);
            updateMemberReqDto.setIsOnline(1);
            return updateMember(updateMemberReqDto);
        }
        CommunMember communMember = ToolsUtil.convertType(reqDto,CommunMember.class);
        communMember.setId(ToolsUtil.getUUID());
        communMember.setIsMuted(0);
        communMember.setCreateTime(LocalDateTime.now());
        communMember.setIsOnline(1);
        return baseMapper.insert(communMember) > 0;
    }

    @Override
    public Boolean updateMember(UpdateMemberReqDto reqDto){
        CommunMember communMember = baseMapper.selectOne(Wrappers.lambdaQuery(CommunMember.class)
                .eq(CommunMember::getNumberId,reqDto.getNumberId())
                .eq(CommunMember::getRoomId,reqDto.getRoomId()));
        if(ParamUtil.notEmpty(reqDto.getIsMuted())){
            communMember.setIsMuted(reqDto.getIsMuted());
        }
        if(ParamUtil.notEmpty(reqDto.getIsOnline())){
            communMember.setIsOnline(reqDto.getIsOnline());
        }
        communMember.setCreateTime(LocalDateTime.now());
        return baseMapper.updateById(communMember) > 0;
    }

    public Boolean checkMember(String numberId,String roomId){
        CommunMember communMember = baseMapper.selectOne(Wrappers.lambdaQuery(CommunMember.class)
                .eq(CommunMember::getNumberId,numberId)
                .eq(CommunMember::getRoomId,roomId));
        if(ParamUtil.notEmpty(communMember)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<QueryMemberResDto> getMember(IdRequestDto reqDto){
        List<CommunMember> communMemberList = baseMapper.selectList(Wrappers.lambdaQuery(CommunMember.class)
                .eq(CommunMember::getIsOnline,1).eq(CommunMember::getRoomId,reqDto.getId()));
        return ToolsUtil.convertType(communMemberList,QueryMemberResDto.class);
    }

    @Override
    public void changeOnline(IdRequestDto reqDto){
        CommunMember communMember = new CommunMember();
        communMember.setIsOnline(0);
        baseMapper.update(communMember,Wrappers.lambdaUpdate(CommunMember.class)
                .eq(CommunMember::getRoomId,reqDto.getId())
                .eq(CommunMember::getIsOnline,1));
    }

    @Override
    public CommunMember getMemberStatus(String numberId,String roomId){
        CommunMember communMember = baseMapper.selectOne(Wrappers.lambdaQuery(CommunMember.class)
                .eq(CommunMember::getNumberId,numberId)
                .eq(CommunMember::getRoomId,roomId));
        return communMember;
    }

}
